package xmetadata

import (
	"context"
	"errors"
	"google.golang.org/grpc/metadata"
	"maintenance/pkg/consts"
	"strconv"
)

func GeMetaDataFromMetaData(ctx context.Context, key string) (string, error) {

	var data string

	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		return data, errors.New("missing metadata")
	}
	dataS := md.Get(key)
	if len(dataS) == 0 {
		return data, errors.New("no data")
	}
	data = dataS[0]
	return data, nil
}

func GeUidFromMetaData(ctx context.Context) (uid int64, err error) {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		return uid, errors.New("missing metadata")
	}
	dataS := md.Get(consts.UserIDKey)
	if len(dataS) == 0 {
		return uid, errors.New("no data")
	}
	data := dataS[0]
	uid, err = strconv.ParseInt(data, 10, 0)
	return
}
